<?php
namespace app\common\model;

class MessageLog extends BaseModel{

	public function getAskIdAttr($relation_id, $data = []){
		$ask_id      = 0;
		$relation_id = $relation_id ? $relation_id : $data['relation_id'];
		$tpl_name    = $data['tpl_name'];
		switch ($tpl_name) {
			case 'comment_tpl':
				$info = db('Comment')->field('module,object_id')->where(['comment_id' => $relation_id])->find();
				if($info['module'] == 'ask'){
					$ask_id = $info['object_id'];
				}
				if($info['module'] == 'ask_answer'){
					$ask_id = db('AskAnswer')->where(['answer_id' => $info['object_id']])->value('ask_id');
				}
				break;

			case 'ask_answer_tpl':
				$ask_id = db('AskAnswer')->where(['answer_id' => $relation_id])->value('ask_id');
				break;

			case 'ask_tpl':
				$ask_id = $relation_id;
				break;

			default:
				# code...
				break;
		}

		return $ask_id;
	}
	// 发送者用户名称
	public function getSendUserNameAttr($user_id = 0, $data = []){
		$user_id = $user_id ? $user_id : $data['send_user_id'];
		if($user_id == '-1'){
			return '匿名';
		}
		return $this->getUserNameAttr($user_id);
	}
	public function getFirstDescAttr($msg_type = '', $data = []){
		$desc     = '';
		$tpl_name = $data['tpl_name'];
		$tpl_key  = $data['tpl_key'];

		$push_tpl = config('message.' . $tpl_name);
		$tpl_data = $push_tpl[$tpl_key];
		$desc = $tpl_data['key_name'];
		if($data['msg_type'] == 'to_me_comment'){
			$desc = '回复';
		}

		return $desc;
	}
	public function getEndDescAttr($msg_type = '', $data = []){
		$relation_id = $data['relation_id'];
		$tpl_name    = $data['tpl_name'];
		$tpl_key     = $data['tpl_key'];
		$desc        = '';
		$msg_type    = $msg_type ? $msg_type : $data['msg_type'];
		switch ($msg_type) {
			case 'comment':
				if($tpl_key == 'answer'){
					$desc = '的回答';
				}
				if($tpl_key == 'answer_reply'){
					$desc = '的评论';
				}
				break;

			case 'to_me_comment':
				if($tpl_key == 'answer_reply'){
					$desc = '的评论';
				}
				break;

			case 'ask_answer':
				$desc = '的问题';
				if($tpl_key == 'answer_submit'){
					$desc = '的问题';
				}
				if($tpl_key == 'answer_reply'){
					$desc = '的回答';
				}
				break;

			case 'notice':
				if($tpl_name == 'ask_tpl'){
					$desc = '的问题';
				}
				if($tpl_name == 'ask_answer_tpl'){
					$desc = '的回答';
				}
				break;

			default:
				# code...
				break;
		}

		return $desc;
	}
}